﻿#include "BrokerLogger.h"
#include "logger\LogBroker.h"
#include "logger\LogMessageInfo.h"

namespace Logging {
BrokerLogger::BrokerLogger(const std::string& name, LogLevel initial_level)
    : name_(name), level_(initial_level) {}

void BrokerLogger::log(LogLevel level, const std::string& message) {
  // 关键优化：在前端进行级别过滤，性能极高
  if (level < level_.load(std::memory_order_relaxed)) {
    return;
  }

  // 级别检查通过，打包并推送到 Broker
  LogMessageInfo msg_info(level, std::string(this->name_),
                          std::string(message));
  LogBroker::GetInstance().Push(std::move(msg_info));
}

void BrokerLogger::set_level(LogLevel level) {
  // 使用原子操作，保证线程安全
  level_.store(level, std::memory_order_relaxed);
}

void BrokerLogger::flush() {
  // LogBroker 应该提供一个 FlushAll Sinks 的方法
  LogBroker::GetInstance().FlushAll();
}

const std::string& BrokerLogger::get_name() const { return name_; }
}  // namespace Logging